package org.singleton.zwideaplugin;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.popup.Balloon;
import com.intellij.openapi.ui.popup.BalloonBuilder;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.ui.JBColor;

import java.awt.*;

public class CodePintAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // TODO: insert action logic here

        //获取当前工程对象
        Project project = e.getData(PlatformDataKeys.PROJECT);

        //获取当前编辑器
        Editor editor = e.getData(PlatformDataKeys.EDITOR);

        //获取选择对象
        SelectionModel selectionModel = editor.getSelectionModel();

        //获取选中的值
        String text = selectionModel.getSelectedText();
        System.out.println(text);

        if(text.equals("") || text ==null){
            return;
        }

        //回到主线程执行 在网咯交互的时候
//        ApplicationManager.getApplication().invokeLater(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });

      //  Messages.showMessageDialog(project,text,"翻译",null);


        //获取Popup工程
        JBPopupFactory factory = JBPopupFactory.getInstance();
        BalloonBuilder builder = factory.createHtmlTextBalloonBuilder(text, null,
                //颜色
                new JBColor(new Color(188, 238, 178), new Color(73, 120, 73)),
                //链接事件
                null);


        builder.setFadeoutTime(5000)//5秒后隐藏
                .createBalloon()//创建气泡
                .show(factory.guessBestPopupLocation(editor),Balloon.Position.below);//显示在下方


    }
}
